import cv2
import numpy as np



# def nothing(x):
#     pass
#
#
# cv2.namedWindow('res')
#
# cv2.createTrackbar('max', 'res', 0, 900, nothing)
# cv2.createTrackbar('min', 'res', 0, 900, nothing)
#
# img = cv2.imread('rgb.jpg', 0)
#
# maxVal = 200
# minVal = 100
#
# while (1):
#
#     if cv2.waitKey(20) & 0xFF == 27:
#         break
#     maxVal = cv2.getTrackbarPos('min', 'res')
#     minVal = cv2.getTrackbarPos('max', 'res')
#     # if minVal < maxVal:
#     #     edge = cv2.Canny(img, 100, 200)
#     #     cv2.imshow('res', edge)
#     # else:
#     edge = cv2.Canny(img, minVal, maxVal)
#     cv2.imshow('res', edge)
# cv2.destoryAllWindows()



# from tkinter import Image
# import os
# from PIL import Image
# import matplotlib.pyplot as plt
# img = Image.open("rgb.jpg")
#
# plt.figure("Image") # 图像窗口名称
# plt.imshow(img)
# plt.axis('on') # 关掉坐标轴为 off
# plt.title('image') # 图像题目
# plt.show()
#


# import cv2
# import ctypes
# from ctypes import *
# import numpy as np
# img = cv2.imread('rgb.jpg',-1)
# dep = cv2.imread('f-1.png',-1)
# ret = np.zeros(dtype=np.uint8,shape=(1024,1024,3))
# dll = ctypes.cdll.LoadLibrary('core3s.dll')
# dll.filter_water(img.ctypes.data_as(ctypes.POINTER(ctypes.c_ubyte)),
#           dep.ctypes.data_as(ctypes.POINTER(ctypes.c_ubyte)),
#           ret.ctypes.data_as(ctypes.POINTER(ctypes.c_ubyte)))
# dll.show()
#
# import cv2
# import numpy as np
# meank = 200
# thresh = 2
# # mouse callback function
# import cv2
# import numpy as np
#
#
# dll.ret_area.restype = c_float
#
# def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
#     if event == cv2.EVENT_LBUTTONDOWN:
#         xy = "%d,%d" % (x, y)
#         area = dll.ret_area(int(x),int(y),meank,c_float(thresh))
#
#         print(area)
# cv2.namedWindow("image")
# cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
#
# while(1):
#     cv2.imshow("image", ret)
#     if (cv2.waitKey(0)&0xFF==27) :
#         break
# cv2.destroyAllWindows()

# class askdll(object):
#     def __init__(self,member):
#         self.dll_lib = ctypes.cdll.LoadLibrary('1.dll')
#         self.member = member
#     def get_mask(self):
#         gray = np.zeros(dtype=np.uint8,shape=(1024,1024,1))
#         self.dll_lib.try_1(self.member.ctypes.data_as(ctypes.POINTER(ctypes.c_ubyte)),
#                            gray.ctypes.data_as(ctypes.POINTER(ctypes.c_ubyte)))
#         return gray
#     def get_area(self,leaf_id):
#
#         return 0
# img = cv2.imread('f-1.jpeg')
# a = askdll(img)
# mask = a.get_mask(2)
# cv2.imshow('',mask)
# cv2.waitKey()
# a = "a b c d\na b c da"
# slice = a.split('\n')[-1].split(' ')[0]
# print(slice)
# img = cv2.imread('f-1.png',-1)
# print(img.shape)
# for i in range(img.shape[0]):
#     for j in range(img.shape[1]):
#         print(img[i,j])
key = 0
img = cv2.imread('cache/out.jpg')
# while 1:
#
#     cv2.imshow('',img)
#     key = cv2.waitKey()
#     msg = '{} is pressed.'.format(chr(key) if key < 256 else key)
#     print(key)

# while 1:
#     img = cv2.imread('cache/out.jpg')
#     cv2.imshow('',img)
#     cv2.waitKey(0)
import  cv2
# img = cv2.imread('1.jpeg')
# img1 = cv2.resize(img,(1024,512))
# cv2.imshow('',img1)
# cv2.waitKey()
# a = np.zeros((2,3))
# print(a)
# from main_realsense import realsense
#
#
#

bgr_img = cv2.imread("./115.jpeg")
gray_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2GRAY)
th, binary = cv2.threshold(gray_img, 0, 255, cv2.THRESH_OTSU)
imgf,contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# cv2.drawContours(bgr_img, contours, -1, (0, 0, 255), 3)

bounding_boxes = [cv2.boundingRect(cnt) for cnt in contours]

for bbox in bounding_boxes:
    [x, y, w, h] = bbox
    if w>52:
        cv2.rectangle(bgr_img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imwrite('bounding.jpg',bgr_img)
cv2.imshow("name", bgr_img)
cv2.waitKey(0)





